const Joi = require('joi');

class ApprovalValidate {
  // 新增审批参数校验
  async insert(params) {

    const schema = Joi.object({
      barcode_id: Joi.string()
        .required()
        .messages({
          'string.empty': '批号编码不能为空',
          'string.required': '批号为必填项',
        }),
      title: Joi.string()
        .required()
        .messages({
          'string.empty': '批号编码不能为空',
          'string.required': '批号为必填项',
        }),
      number: Joi.string()
        .required()
        .messages({
          'string.empty': '数量不能为空',
          'string.required': '数量为必填项',
        }),
      type: Joi.number()
        .required()
        .messages({
          'number.empty': '类型不能为空',
          'number.required': '类型为必填项',
        }),
      status: Joi.number().default(0),
      product_id: Joi.string()
        .required()
        .messages({
          'string.empty': '产品id不能为空',
          'string.required': '产品id为必填项',
        }),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 拒绝审批参数校验
  async refuse(params) {
    const schema = Joi.object({
      approval_id: Joi.string()
        .required()
        .messages({
          'string.empty': '审批id不能为空',
          'string.required': '审批id为必填项',
        }),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 同意审批参数校验
  async pass(params) {

    const schema = Joi.object({
      approval_id: Joi.string()
        .required()
        .messages({
          'string.empty': '审批id不能为空',
          'string.required': '审批id为必填项',
        }),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 审批历史参数校验
  async list(params) {

    const schema = Joi.object({
      name: Joi.string(),
      barcode: Joi.string(),
      type: Joi.string(),
      status: Joi.number(),
      order: Joi.string().default('create_time'),
      method: Joi.string().default('desc'),
      pageIndex: Joi.string().default('1'),
      pageSize: Joi.string().default('10'),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 审批详情参数校验
  async info(params) {

    const schema = Joi.object({
      id: Joi.string(),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }
}

module.exports = new ApprovalValidate();
